/*
*  Des.enc("password");
* */
use([
    "public/javascripts/service/loginService.js",
    "public/javascripts/widget/prompt/prompt.js",
    "public/common/des.js"
], function(LoginService, PromptComponent, Des) {
var PageComponent = React.createClass({
    eventObj: {},
    componentDidMount: function() {
        $("#userName").attr('maxlength','30');
        $("#passWord").attr('maxlength','18');
        $("#vertifyResult").attr('maxlength','4');
        // if(cookie.get('token')) {
        //     cookie.delete('token');
        //     cookie.delete('username');
        //     cookie.delete("userId");
        //     cookie.delete("userType");
        //     cookie.delete("primary");
        //     cookie.delete("fullName");
        //     cookie.delete("loginSuccess");
        //     cookie.delete("sex");
        //     //window.location.reload();
        // }
        if(location.href.indexOf("errorCode=401") > -1) {
            PromptComponent.init({'type': 'warning', "msg": "请登录!"}).show();
        }
    },
    getInitialState: function () {
        return {
            userName: '',
            passWord:'',
            validateCode:'',
            errorsMsg:'',
            vertifyImage:'',
            imageUrl:'',
            loginTime:0

        };

    },
    refuseSubmit:function(e){
        e.preventDefault();
        return false;

    },
    ChangeValue: function(event) {
        var name = event.target.name;
        var value = event.target.value;
        this.eventObj[name] = value;
        this.setState(this.eventObj);
    },
    vertifyStyle:function () {
        styleObj = {};
        if(this.state.errorsMsg){
            styleObj = {display: 'block'};
        }else{
            styleObj = {display: 'none'};
        }
        return styleObj;
    },
    changeVertifyImage:function () {
        this.setState({imageUrl: Interface.getUrlById("login.getcodeimage") + "?t=" + new Date().getTime()});
    },
    summit:function () {
        var that = this;

        /*获取*/
        if(!this.state.userName) {
            return;
        }

        if(!this.state.passWord) {
            return;
        }


        if(this.state.loginTime !=0  && !this.state.validateCode){
            return;
        }

        var param = {
            passWord: Des.enc(this.state.passWord),
            userName:this.state.userName,
            validateCode:this.state.validateCode
        };

        LoginService.login(param,function (returnData) {
            that.setState({loginTime:1});
            if(returnData.errorCode != 0 ) {
                if(returnData.errorCode == 110010){
                    /*主账号未提交申请*/
                    cookie.set("token", returnData.data.token, 1);//跳转
                    window.location.href = "/resetChangePw";
                    return;
                }
                if(returnData.errorCode == 110008){
                    /*主账号未提交申请*/
                    cookie.set("token", returnData.data.token, 1);//跳转
                    window.location.href = "/registerUncomplete";
                    return;
                }
                if(returnData.errorCode == 110002){
                    window.location.href = "/lockedPage";
                    return;
                }
                that.setState({errorsMsg: returnData.msg});
                that.changeVertifyImage();

            }else{
                that.setState({errorsMsg: ''});
                cookie.set("token", returnData.data.token, 1);//跳转
                cookie.set("username", that.state.userName, 1);
                cookie.set("userId", returnData.data.userId, 1);
                cookie.set('tradingCurrency', returnData.data.tradingCurrency, 1);
                localStorage.removeItem('avatar');

                //1：物流客户；2：贸易客户；256：贸易供应商
                cookie.set("userType", returnData.data.userType, 1);
                cookie.set("primary",returnData.data.primary, 1);    //主账号true,子账号false
                var switchDataUrl = easyMe.getQueryString("switchDataUrl");
                if(switchDataUrl) {
                    switchDataUrl = decodeURIComponent(switchDataUrl);
                    var ssoLoginUrl = "http://" + switchDataUrl.split("/")[2] + "/ssoLogin";
                    var url = ssoLoginUrl + '?token=' + returnData.data.token + "&fromUrl=" + encodeURIComponent(switchDataUrl);
                    $("#sso-login").attr("src", url);
                    return;
                } else {
                    var ssoLoginUrl = "http://" + Api.EDIHost + ":" + Api.EDIPort + "/ssoLogin";
                    var url = ssoLoginUrl + '?token=' + returnData.data.token;
                    $("#sso-login").attr("src", url);
                    $("#sso-login").on("load", function() {
                        gotoNextPage();
                    });
                }
                function gotoNextPage() {
                    var redirectUrl = easyMe.getQueryString("redirectUrl");
                    if (!redirectUrl) {
                        if (returnData.data.userType == 2) {
                            window.location.href = "/trdOrdersList";
                        } else if (returnData.data.userType == 256) {
                            window.location.href = "/order_manage_trade";
                        } else {
                            window.location.href = "/product_list";
                        }
                    } else {
                        var redirectUrl = decodeURIComponent(redirectUrl);
                        if (redirectUrl == '/trdOrdersList') {
                            if (returnData.data.userType == 2) {
                                window.location.href = '/trdOrdersList';
                            } else if (returnData.data.userType == 1 || returnData.data.userType == 3) {
                                window.location.href = '/product_list';
                            } else {
                                window.location.href = '/';
                            }
                        } else {
                            window.location.href = redirectUrl;
                        }
                    }
                }
            }
        });

        that.setState({validateCode:''});


        event.preventDefault();
    },
    render:function() {
        var data = this.state.data;
        return ~init;
    }

});
if(typeof ReactDOM != "undefined") {
    ReactDOM.render(<PageComponent data={storage}/>, document.getElementById('container'));
}
module.exports = PageComponent;
});
